#!/usr/bin/env python
# -*- coding:utf-8 -*-
from spire.presentation.common import *
from spire.presentation import *


class PPTGeneration:
    def __init__(self, template_path):
        self.template_path = template_path
        self.template = Presentation()
        self.template.LoadFromFile(self.template_path)
        self.ppt = Presentation()
        self.ppt.Slides.RemoveAt(0)

    def generate_ppt(self, template_index, elements):
        """
            生成PPT页面
        """
        self.ppt.Slides.AppendBySlide(self.template.Slides[template_index])
        slide = self.ppt.Slides[len(self.ppt.Slides) - 1]
        current_elements = []
        for shape in slide.Shapes:
            if isinstance(shape, IAutoShape) and shape.TextFrame.Text:
                text_content = shape.TextFrame.Text
                current_elements.append(
                    {"type": "TextBox", "instance": IAutoShape, "shape": shape, "content": text_content})

            elif isinstance(shape, SlidePicture):
                current_elements.append(
                    {"type": "Image", "instance": SlidePicture, "shape": shape, "content": shape.Name})

            elif isinstance(shape, PictureShape):
                current_elements.append({"type": "Image", "instance": PictureShape, "shape": shape, "content": None})

            elif isinstance(shape, ITable):
                current_elements.append({"type": "Table", "instance": ITable, "shape": shape, "content": None})

        print(current_elements)
        print(elements)
        print(f"current_elements: {len(current_elements)} elements: {len(elements)}")
        for element in elements:
            target_element = current_elements[element["element_idx"]]
            if target_element["type"] == "TextBox":
                target_element["shape"].TextFrame.Text = element['content']
            elif target_element["type"] == "Image":
                # TODO save image
                target_element["shape"].Name = element['content']
            elif target_element["type"] == "Table":
                # TODO save image
                table_data = element['content']

    def save_ppt(self, save_path, ppt_type=FileFormat.Pptx2019):
        """
            保存PPT
        """
        self.ppt.SaveToFile(save_path, ppt_type)
        self.ppt.Dispose()
        self.template.Dispose()
        self.remove_warnings(save_path)

    def remove_warnings(self, file_path):
        """
            去除警告
        """
        from pptx import Presentation
        prs = Presentation(file_path)

        for slide in prs.slides:
            for shape in slide.shapes:
                if shape.has_text_frame:
                    for paragraph in shape.text_frame.paragraphs:
                        text = paragraph.text
                        if 'Evaluation Warning : The document was created with Spire.Presentation for Python' in text:
                            sp = shape._element
                            sp.getparent().remove(sp)

        prs.save(file_path)
